if (!ie) var ie = window.navigator.userAgent.indexOf ("MSIE") != -1;
function PopupMenu (id)
{
    var self = this;
    function build ()
    {
        panel = document.createElement ("table");
        panel.style.position = "absolute";
        panel.style.border = "1px outset #CCCCCC";
        panel.style.backgroundColor = 'lightyellow';
        panel.style.zIndex = 200;
        panel.id = id;
        panel.cellPadding = 1;
        panel.cellSpacing = 0;

        if (!document.PopupInit)
        {
            document.PopupInit = true;
            var backup = document.onclick;
            document.onclick = function (e)
            {
                self.close();
                if (backup) backup (e);
            }
/*
            var right_backup = document.oncontextmenu;
            document.oncontextmenu = function ()
            {
                self.close();
                if (right_backup) return right_backup ();
                return true;
            }
*/
        }
    }
    this.id = id;
    this.source = null;
    var panel = null;
    var items = [], namedItems = [];

    build ();

    this.show = function (x, y)
    {
        var p = document.getElementById (id);
        if (!p) document.body.appendChild (panel);

        var width = panel.offsetWidth;
        var height = panel.offsetHeight;
        var rightedge = document.body.clientWidth - x;
        var bottomedge = document.body.clientHeight - y;

        if (rightedge < width)
            panel.style.left = (document.body.scrollLeft + x - width) + "px";
        else
            panel.style.left = (document.body.scrollLeft + x) + "px";
        if (bottomedge < height)
            panel.style.top  = (document.body.scrollTop + y - height) + "px";
        else
            panel.style.top = (document.body.scrollTop + y) + "px";
        panel.style.display = ie ? "block" : "table";
    }

    this.close = function ()
    {
        panel.style.display = "none";
        panel.style.left = 0;
        panel.style.top = 0;
    }

    this.add = function (item)
    {
        item.draw (panel);
        items.push (item);
        namedItems [item.id] = item;
        item.popup = this;
    }

    this.getMenuItem = function (id)
    {
        return namedItems [id];
    }

    this.addSplit = function ()
    {
        var row = panel.insertRow (-1);
        var cell = row.insertCell (-1);
        cell.bgColor = "#EEEEEE";
        row.height = 1;
        cell.height = 1;
        cell = row.insertCell (-1);
        cell.height = 1;
        cell.bgColor = "#CCCCCC";
    }
}

function MenuItem (id, label, icon, handler)
{
    this.id = id;
    this.label = label;
    this.icon = icon;
    this.handler = handler;
    this.popup = null;
    var self = this;

    var LabelCell = null;
    var row = null;

    this.draw = function (table)
    {
        row = table.insertRow (-1);
        var cell = row.insertCell (-1);
        row.style.cursor = "default";
        cell.bgColor = "#EEEEEE";
        cell.width = 20;
        cell.noWrap = true;
        cell.align = "center";
        if (icon)
        {
            var img = document.createElement ("img");
            img.src = icon;
            cell.appendChild (img);
        }
        cell = row.insertCell (-1);
        LabelCell = cell;
        cell.style.lineHeight = "150%";
        cell.style.borderLeft = "1px solid #CCCCCC";
        cell.innerHTML = label + "&nbsp;";
        cell.style.textIndent = "5px";
        this.enable ();
    }

    this.disable = function ()
    {
        row.style.color = 'gray';
        if (ie) row.disabled = true;
        row.onmouseover = "";
        row.onmouseout = "";
        row.onclick = "";
    }

    this.enable = function ()
    {
        row.style.color = "";
        if (ie) row.disabled = false;
        row.onclick = function (event)
        {
            event = event || window.event;
            var e = {
                x : event.clientX,
                y : event.clientY,
                popupMenu: self.popup,
                menuItem: self,
                source:self.popup.source
            };
            try {
                setTimeout (function () {
                    if (self.handler != null) {
                        self.handler (e);
                    }
                }, 0);
            } finally {
                self.popup.close ();
            }
        }
        row.onmouseover = function (event)
        {
            var e = event || window.event;
            var target = e.target || e.srcElement;
            target.parentNode.style.backgroundColor = "#5378E8";
            target.parentNode.style.color = 'white';
        }
        row.onmouseout = function (event)
        {
            var e = event || window.event;
            var target = e.target || e.srcElement;
            target.parentNode.style.backgroundColor = "";
            target.parentNode.style.color = '';
        }
    }
}